package com.xing.servlet;

import com.xing.domain.Book;
import com.xing.domain.User;
import com.xing.service.BookService;
import com.xing.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

/*
 * 静，谦虚谨慎，心胸宽广容纳所有好的坏的、先苦后甜
 * day40web1
 * 2021/4/30 14:52
 */
@WebServlet(value = "/booklists")
public class BookListServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //判断是否登录
        User user = (User) req.getSession().getAttribute("user");
        if(user==null){
            resp.sendRedirect(req.getContextPath()+"/login.html");
            return;
        }



        //查询所有数据
        BookService bookService = new BookServiceImpl();
        List<Book> booklist = bookService.findAll();
        req.setAttribute("bl", booklist);
        //转发
        req.getRequestDispatcher("/booklist.jsp").forward(req, resp);


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
